package com.tiger.cloud.controller.crm;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tiger.cloud.common.mybatis.BaseController;
import com.tiger.cloud.enums.DeleteEnum;
import com.tiger.cloud.enums.RecommandListAcceptStatus;
import com.tiger.cloud.enums.RecommandListStatus;
import com.tiger.cloud.filter.CurrentUserId;
import com.tiger.cloud.filter.CurrentUserLoginId;
import com.tiger.cloud.filter.IgnoreAuth;
import com.tiger.cloud.model.crm.MailList;
import com.tiger.cloud.model.crm.RecommandList;
import com.tiger.cloud.service.crm.IMailListService;
import com.tiger.cloud.service.crm.IRecommandListService;
import com.tiger.cloud.util.ErrorCode;
import com.tiger.cloud.util.Page;
import com.tiger.cloud.util.Response;
import com.tiger.cloud.util.ResponseUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 好友推荐/申请
 */
@Controller
@RequestMapping(value = "recommandList")
@Api(value = "好友推荐/申请",tags = "好友推荐/申请")
public class RecommandListController extends BaseController {

	@Autowired
	private IRecommandListService recommandListService;
	@Autowired
	private IMailListService mailListService;
	
	/**
	 * 增量分页获取要事纪要
	 */
	@RequestMapping(value = "find", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "增量分页获取推荐/申请",notes = "增量分页获取要推荐/申请")
	public Response find(@ApiParam(name="id",value="用户id",hidden=true) @CurrentUserId Long userId,
			@RequestBody RecommandList recommandList){
		recommandList.getStatusIn().add(RecommandListStatus.PEND_VERIF);//待验证
		recommandList.getStatusIn().add(RecommandListStatus.CREATED);//已经添加
		recommandList.setUserIdOr(userId);
		if(recommandList.isHasAddressbook()){//通讯录请求
			recommandList.getStatusIn().add(RecommandListStatus.WAIT_CREATE);//待添加
		}else{
			recommandList.getAcceptStatusIn().add(RecommandListAcceptStatus.WAIT_ACCEPT);//已接受
			recommandList.getAcceptStatusIn().add(RecommandListAcceptStatus.ACCEPTED);//已接受
			recommandList.setIsDelete(DeleteEnum.NOMAIL);
			recommandList.setAcceptIsDelete(DeleteEnum.NOMAIL);
		}
		Page<RecommandList> datas=recommandListService.page(recommandList);
		datas.setCurrentUserId(userId);
		return ResponseUtil.ok(datas);
	}
	
	/**
	 * 增量好友推荐/申请
	 */
	@RequestMapping(value = "incrementRecommands", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "增量好友推荐/申请",notes = "增量好友推荐/申请")
	public Response incrementRecommands(@ApiParam(name="id",value="用户id",hidden=true) @CurrentUserId Long userId,@RequestBody RecommandList recommandList){
		recommandList.setIsDelete(DeleteEnum.NOMAIL);
		recommandList.setAcceptIsDelete(DeleteEnum.NOMAIL);
		recommandList.setUserIdOr(userId);
		List<RecommandList> recommandLists =recommandListService.find(recommandList);
		return ResponseUtil.ok(recommandLists);
	}
	
	/**
	 * 运行通讯录匹配
	 */
	@RequestMapping(value = "runMailListsMatch", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "运行通讯录匹配",notes = "运行通讯录匹配")
	@IgnoreAuth
	public Response runMailListsMatch(@ApiParam(name="id",value="用户id",hidden=true) @CurrentUserId Long userId){
		mailListService.runMailListsMatch(userId);
		return ResponseUtil.ok();
	}
	
	/**
	 * 清空运行通讯录匹配
	 */
	@RequestMapping(value = "clearMailListsMatch", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "清空运行通讯录匹配",notes = "清空运行通讯录匹配")
	@IgnoreAuth
	public Response clearMailListsMatch(){
		mailListService.clearMailListsMatch();
		return ResponseUtil.ok();
	}
	
	/**
	 * 发送通讯录到服务器
	 */
	@RequestMapping(value = "sendMailList", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "发送通讯录到服务器",notes = "发送通讯录到服务器")
	public Response sendMailList(@ApiParam(name="id",value="用户id",hidden=true) @CurrentUserId Long userId,@RequestBody HashSet<String> mobiles,@ApiParam(name="id",value="用户id",hidden=true) @CurrentUserLoginId String  loginId){
		if(mobiles==null||mobiles==null||mobiles.size()==0){
			return ResponseUtil.error(ErrorCode.PARAMETER_INVALID,"Mobiles 不能为空！");
		}
		List<MailList> mailLists=new ArrayList<MailList>();
		for (String mobile : mobiles) {
			MailList mailList=new MailList();
			mailList.setUserId(userId);
			if((!StringUtils.isEmpty(mobile)) && (!loginId.equals(mobile))){
				mailList.setMobile(mobile);
				mailLists.add(mailList);
			}
		}
		try {
			if(mailLists.size()==0){
				return ResponseUtil.ok();
			}
			if(mailListService.addMailLists(mailLists, userId)){
				mailListService.runMailListsMatch(userId);
				return ResponseUtil.ok();
			};
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseUtil.ok();
	}
	
	
	/**
	 * 同意申请
	 */
	@RequestMapping(value = "acceptRequest", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "同意申请",notes = "同意申请")
	public Response acceptRequest(@ApiParam(name="id",value="用户id",hidden=true) @CurrentUserId Long userId,
			HttpServletRequest request,@RequestBody  RecommandList recommandList){
		if(recommandList.getId()!=null&&recommandList.getId()>0){
			RecommandList findRecommandList=recommandListService.get(recommandList.getId());
			if(findRecommandList==null){
				return ResponseUtil.error(ErrorCode.OBJECT_NOT_EXITS,"请求添加不存在!");
			}
			if(findRecommandList.getAcceptStatus()==RecommandListAcceptStatus.ACCEPTED){
				return ResponseUtil.error("已经添加");
			}
			if(recommandListService.acceptRequest(findRecommandList,userId)){
				return ResponseUtil.ok();
			}
			return ResponseUtil.error();
		}
		return ResponseUtil.error("参数错误,请重试!");
	}
	
	/**
	 * 申请同行为好友
	 */
	@RequestMapping(value = "recommandUser", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "扫码/手机号申请好友",notes = "扫码/手机号申请好友")
	public Response recommandUser(@ApiParam(name="id",value="用户id",hidden=true) @CurrentUserId Long userId,HttpServletRequest request,@RequestBody  RecommandList recommandList){
		if(StringUtils.isEmpty(recommandList.getRemark())){
			recommandList.setRemark("请求添加为好友");
		}
		if(recommandList.getRecommandId()==null||recommandList.getRecommandId()<0){
			return ResponseUtil.error(ErrorCode.PARAMETER_INVALID,"recommandId必传!");
		}
//		if(recommandList.getRecommandWay()==null||recommandList.getRecommandWay()==ResourceTypeEnum.ADDRESSBOOK){
//			return ResponseUtil.error(ErrorCode.PARAMETER_INVALID,"recommandWay必须是0(CRM用户匹配-->好友申请)/1(扫码)");
//		}
		recommandList.setUserId(userId);
		recommandList.setAcceptDate(null);
		recommandList.setRecommandDate(new Date());
		recommandList.setStatus(RecommandListStatus.PEND_VERIF);
		recommandList.setRemark(recommandList.getRemark());
		recommandList.setAcceptStatus(RecommandListAcceptStatus.WAIT_ACCEPT);
		if(recommandListService.recommandUser(recommandList,userId)){
			return ResponseUtil.ok();
		}
		return ResponseUtil.error();
	}

	/**
	 * 删除通讯录
	 */
	@RequestMapping(value = "delete", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "删除申请记录",notes = "删除申请记录")
	public Response delete(@ApiParam(name="id",value="用户id",hidden=true) @CurrentUserId Long userId,@RequestBody  RecommandList recommandList){
		if(recommandList.getId()!=null&&recommandList.getId()>0){
			RecommandList deleteEntity=recommandListService.get(recommandList.getId());
			if(deleteEntity==null||deleteEntity.getRecommandId().longValue() != userId.longValue()){
				return ResponseUtil.error("参数错误,请重试!");
			}
			if(deleteEntity.getAcceptStatus() == RecommandListAcceptStatus.ACCEPTED){
				return ResponseUtil.error("该用户已添加,无法删除!");
			}
			RecommandList  recommand = new RecommandList();
			recommand.setId(recommandList.getId());
			if(recommandListService.remove(recommand)>0){
				return ResponseUtil.ok();
			}
			return ResponseUtil.error();
		}
		return ResponseUtil.error("参数错误,请重试!");
	}

}